package com.crm.service;

import com.crm.entity.Svr;
import com.crm.repository.ServiceRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;


@Service("svrService")
public class SvrServiceImpl implements SvrService {

    @Resource
    ServiceRepository serviceRepository;

    @Override
    public Page<Svr> findAll(String svrCustName,String svrTitle, String svrType, Pageable pageable) {
        Specification<Svr> specification = new Specification<Svr>() {
            @Override
            public Predicate toPredicate(Root<Svr> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (svrCustName != null && !svrCustName.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("svrCustName"), "%" + svrCustName + "%"));
                }
                if (svrTitle != null && !svrTitle.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("svrTitle"), "%" + svrTitle + "%"));
                }
                if (svrType != null && !svrType.equals("")) {
                    predicates.add(criteriaBuilder.equal(root.get("svrType"), svrType));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return serviceRepository.findAll(specification, pageable);
    }

    @Override
    public Page<Svr> findAllByPage(String svrCustName, String svrDueTo, String svrTitle, Pageable pageable) {
        Specification<Svr> specification = new Specification<Svr>() {
            @Override
            public Predicate toPredicate(Root<Svr> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (svrCustName != null && !svrCustName.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("svrCustName"), "%" + svrCustName + "%"));
                }
                if (svrDueTo != null && !svrDueTo.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("svrDueTo"), "%" + svrDueTo + "%"));
                }
                if (svrTitle != null && !svrTitle.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("svrTitle"), "%" + svrTitle + "%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return serviceRepository.findAll(specification, pageable);
    }

    @Override
    public Page<Svr> findByIdArchived(String svrCustName, String svrTitle, String svrType, Pageable pageable) {
        Specification<Svr> specification = new Specification<Svr>() {
            @Override
            public Predicate toPredicate(Root<Svr> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (svrCustName != null && !svrCustName.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("svrCustName"), "%" + svrCustName + "%"));
                }
                if (svrTitle != null && !svrTitle.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("svrTitle"), "%" + svrTitle + "%"));
                }
                if (svrType != null && !svrType.equals("")) {
                    predicates.add(criteriaBuilder.equal(root.get("svrType"), svrType));
                }
                predicates.add(criteriaBuilder.equal(root.get("svrStatus"), "已归档"));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return serviceRepository.findAll(specification, pageable);
    }

    @Override
    public void del(Integer svrId) {
        serviceRepository.deleteById(svrId);
    }

    @Override
    public void save(Svr svr) {
        serviceRepository.save(svr);
    }

    @Override
    public Svr getServiceBySvrId(Integer svrId) {
        return serviceRepository.findById(svrId).get();
    }
}
